package com.mes.cotask.manager;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mes.common.spring.SpringUtils;
import com.mes.common.util.SysUtil;
import com.mes.cotask.dao.iface.ICotaskDao;
import com.mes.cotask.manager.iface.ICotaskManager;
import com.mes.cotask.manager.iface.ISchedulerInteg;
import com.mes.cotask.domain.Cotask;
import com.mes.schedule.manager.iface.IScheduleEventManager;
import com.mes.task.manager.iface.IOperationTaskManager;
import com.mes.task.domain.OperationTask;

@Component("schedulerInteg")
public class SchedulerInteg implements ISchedulerInteg {

	@Autowired
	private SysUtil sysUtil;
	@Autowired
	private IScheduleEventManager scheduleEventManager;
	@Autowired
	private ICotaskDao cotaskDao;
	@Autowired
	private IOperationTaskManager operationTaskManager;

	@Override
	public void feedbackCotask(List<OperationTask> operationTasks) throws Exception {
		if ("on".equals(sysUtil.getProperty("coSchedule"))) {
			return;
		}

		for (OperationTask opTask : operationTasks) {
			operationTaskManager.lockOperationTask(opTask.getTaskUid(), opTask.getPlanStart(),
					opTask.getPlanFinish(), opTask.getNotes());
		}

	}

	@Override
	public void feedbackCotask(Cotask cotask) throws Exception {
		if ("on".equals(sysUtil.getProperty("cotask.feedbackToScheduler.parameters.coSchedule"))) {
			return;
		}
		// 新旧cotaskmanager居然就只是大小写不同
		// 很不规范
		ICotaskManager cotaskManager = (ICotaskManager) SpringUtils.getBean("CotaskManager");
		List<OperationTask> opTaskList = cotaskManager.getAllSubTasks(cotask);
		if (opTaskList != null && opTaskList.size() > 0) {
			for (OperationTask opTask : opTaskList) {
				// Cotask cotask =
				// this.getCotaskDao().findByTaskUid(opTask.getTaskUid());
				operationTaskManager.lockOperationTask(opTask.getTaskUid(), cotask.getScheduleStart(),
						cotask.getScheduleFinish(), cotask.getNotes());
			}
		}

	}
}
